package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"mouse-ctrl-sys/cache"
	"mouse-ctrl-sys/models"
	"time"
)

func UserList(c *gin.Context) {
	/*
				SELECT
			 roles.id as Aa,
		  roles.role_name as Bb
			FROM
			 roles
			  LEFT JOIN user_roles on user_roles.role_id=roles.id
	*/
	var list []models.Cc
	redis_list_key := "redis_list_"
	result, _ := cache.Rdb.Get(cache.Ctx, redis_list_key).Result()
	if result == "" {
		models.DB.Table("roles").
			Select("roles.id as Aa", "roles.role_name as Bb").
			Joins("LEFT JOIN user_roles on user_roles.role_id=roles.id").
			Debug().
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_list_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}
func RoleList(c *gin.Context) {
	/*

		SELECT
		  *
		FROM
		  quans
		  LEFT JOIN role_quans ON role_quans.quan_id = quans.id
	*/
	var list []models.Quan
	redis_lists_key := "redis_lists_"
	errCount, _ := cache.Rdb.Get(cache.Ctx, redis_lists_key).Result()
	if errCount == "" {
		models.DB.Table("quans").
			Joins("LEFT JOIN role_quans ON role_quans.quan_id = quans.id").
			Debug().
			Find(&list)
		jsonData, _ := json.Marshal(&list)
		cache.Rdb.Set(cache.Ctx, redis_lists_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(errCount), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": list,
	})
	return
}
